<?php
class MozgProperty
{
	private $_data = array();
	
	public function __call($name, $args)
	{
		$lName = $this->lName($name);
		if (0 === strpos($lName, 'set'))
		{
			return $this->Set(substr($name, 3), $args[0]);			
		}
		else if (0 === strpos($lName, 'get')) 
		{
			$name = substr($name, 3);
			if (isset($args[0])) $name .= '/' .(string)$args[0];
			return $this->Get($name);			
		}
		else
		{
			throw new Exception('Неизвестный метод');
		}
	}
	
	public function __set($name, $value)
	{
		return $this->{'set'.(string)$name}($value); 	
	}
	
    public function __get($name)
    {
		return $this->{'get'.(string)$name}(); 
    }
    
    /**
		сеттер по умолчанию
	*/
	public function Set($name, $value)
	{
		$nameParts = explode('/', $this->lName($name));
		$data = &$this->_data;
		while (($name = array_shift($nameParts)) && is_array($data))
		{
			if (!isset($data[$name])) $data[$name] = array(); 
			$data = &$data[$name]; 
		}		
		
		$data = $value;
		return $this;
	}
	
    /**
		геттер по умолчанию
	*/	
    public function Get($name)
    {
    	$nameParts = explode('/', $name, 2);
    	$name = array_shift($nameParts);
    	
        $value = $this->HasSet($name) ? $this->_data[$this->lName($name)] : null;

        return $this->GetPath(isset($nameParts[0]) ? $nameParts[0] : '', $value);        
    } 	
    
    protected function GetPath($path, $data)
    {
    	$nameParts = explode('/', $path);
		while (($name = array_shift($nameParts)) && is_array($data) && isset($data[$name]))
        {
        	$data = $data[$name];
        }    	
        
        return $data;
    }
    
    public function HasSet($name)
    {
    	return isset($this->_data[$this->lName($name)]);
    }
    
	public function Delete($key)
	{
		if ($this->HasSet($key)) unset($this->_data[$this->lName($name)]);
		return $this;
	}  

	public function GetData()
	{
		return $this->_data;
	}
	
	public function SetData($data)
	{
		$this->_data = $this->lData((array)$data);
		return $this;
	}
	
    public function AddData()
    {
    	$args = func_get_args();

		foreach ($args as $data)
		{		
			if (!is_array($data)) continue;	
			$this->_data = $this->lData($data) + $this->_data;	
		}
		
		return $this;
    }	
	
	public function ClearData()
	{
		$this->_data = array();
		return $this;
	}

	protected function lData(array $data)
	{
		return array_change_key_case($data, CASE_LOWER);
	}	
	
	protected function lName($name)
	{
		return strtolower($name);
	}
}